package com.andy.qhb.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.andy.qhb.QhbApplication;
import com.andy.qhb.base.RespVO;
import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.dao.vo.Where;
import com.andy.qhb.entity.ConfCommon;
import com.andy.qhb.game.GameUtils;
import com.andy.qhb.game.GameConf;

/**
 * @Description: 接受后台命令处理
 * @author cuiwm
 */
@Controller
@RequestMapping("admin/cmd")
public class CmdController extends BaseController{

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@RequestMapping("refreshCache/{type}")
	public @ResponseBody RespVO refreshCache(@PathVariable("type") String type,String seckey) {
		if(!checkSafe(seckey)){
			return RespVO.createErrorJsonResonse("非法操作");
		}
		logger.error("收到后台命令："+type);
		if(type.equals("2")){
			// 参数配置
			GameConf.initCommons();
		}else if(type.equals("3")){
			// 停服
			QhbApplication.destroy();
		}
		
		return RespVO.createSuccessJsonResonse("执行成功");
	}

	private boolean checkSafe(String seckey) {
		ConfCommon conf = DaoUtils.getOne(ConfCommon.class, Where.eq("nid", "commu_seckey"));
		return (conf != null && conf.getValue().equals(seckey));
	}
	
	@RequestMapping("addCoin/{id}")
	public @ResponseBody RespVO addCoin(@PathVariable("id") Long id,Long uid,Integer addCoin,String seckey) {
		if(!checkSafe(seckey)){
			return RespVO.createErrorJsonResonse("非法操作");
		}
		logger.error("收到后台命令addCoin：[{}]=[{}]",id,addCoin);
		GameUtils.addCoin(uid, addCoin);
		
		return RespVO.createSuccessJsonResonse("执行成功");
	}
	
	
	@RequestMapping("test/anli")
	public @ResponseBody RespVO anli() {
		
		// 消费信息
//		String day = DateUtil.getCurrentDateStr();
//		DaoUtils.update("insert into usr_day_rept(uid,win_coin,day) SELECT * FROM (select t.uid,sum(coin),'"+day+"' from usr_fab_log t group by t.uid) as tb");
//		
//		// 页节点
//		List<UsrUser> yeJds = DaoUtils.getListSql(UsrUser.class, "select id from usr_user t where not EXISTS (select 1 from usr_user u where u.puid = t.id) and t.puid != 0");
//		for(UsrUser ye:yeJds){
//			// 当日消费
//			UsrDayRept dayRept = DaoUtils.getOneSql(UsrDayRept.class, "select uid,sum(coin) as win_coin from usr_day_rept where uid = ?", ye.getId());
//			DaoUtils.insert(dayRept);
//			
//			
//		}
		
		return RespVO.createSuccessJsonResonse("执行成功");
	}
	
}
